package br.webcrawler.activities.webcrawler.webcrawler;


import br.webcrawler.activities.webcrawler.utils.Data;
import br.webcrawler.activities.webcrawler.webcrawler.WebCrawler;
import br.webcrawler.activities.webcrawler.xml.Constants;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.UIManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Gerson
 */
public class MainWindow extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = -4954784446020533308L;
	private JFileChooser tcxFile;
    private JFileChooser xmlFile;
    private JFileChooser gpxFile;
    private String tcxPath = "";
    private String xmlPath = "";
    private String gpxPath = "";
    private boolean customDatesSelected = false;
    
    /**
     * Creates new form MainWindow
     */
    public MainWindow() {
        try {
                // Set System L&F
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
                // handle exception
        }
        initComponents();
        tcxFile = new JFileChooser();
        xmlFile = new JFileChooser();
        gpxFile = new JFileChooser();
        tcxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        xmlFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        gpxFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
       
        
        createComponents();
        
        
//        ex.addItem(opt);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        mainPanel = new javax.swing.JPanel();
        loginPanel = new javax.swing.JPanel();
        jlUsername = new javax.swing.JLabel();
        jtfUsername = new javax.swing.JTextField();
        jlPassword = new javax.swing.JLabel();
        jpfPassword = new javax.swing.JPasswordField();
        explorePanel = new javax.swing.JPanel();
        jlKeyword = new javax.swing.JLabel();
        jtfKeyword = new javax.swing.JTextField();
        jlLocation = new javax.swing.JLabel();
        jtfLocation = new javax.swing.JTextField();
        jbSearch = new javax.swing.JButton();
        filtersPanel = new javax.swing.JPanel();
        jlActivityType = new javax.swing.JLabel();
        jcbActivityType = new ExtendedComboBox();
        jlEventType = new javax.swing.JLabel();
        jcbEventType = new javax.swing.JComboBox();
        jlTimePeriod = new javax.swing.JLabel();
        jcbTimePeriod = new javax.swing.JComboBox();
        jpCustomDates = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jdcDateInit = new com.toedter.calendar.JDateChooser();
        jdcDateEnd = new com.toedter.calendar.JDateChooser();
        jLabel2 = new javax.swing.JLabel();
        jlDistance = new javax.swing.JLabel();
        jtfDistanceInit = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfDistanceEnd = new javax.swing.JTextField();
        jlTime = new javax.swing.JLabel();
        jtfTimeInit = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jtfTimeEnd = new javax.swing.JTextField();
        jlElevationGain = new javax.swing.JLabel();
        jtfElevationInit = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfElevationEnd = new javax.swing.JTextField();
        jlDisplayName = new javax.swing.JLabel();
        jtfDisplayName = new javax.swing.JTextField();
        jcbMyActivities = new javax.swing.JCheckBox();
        jpStorage = new javax.swing.JPanel();
        jlTcx = new javax.swing.JLabel();
        jtfTcx = new javax.swing.JTextField();
        jlXml = new javax.swing.JLabel();
        jtfXml = new javax.swing.JTextField();
        jbTcxFolder = new javax.swing.JButton();
        jbXmlFolder = new javax.swing.JButton();
        jlGpx = new javax.swing.JLabel();
        jtfGpx = new javax.swing.JTextField();
        jbGpxFolder = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Garmin Web Crawler - Test");
        setResizable(false);

        loginPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Sign in with your Garmin account"));
        loginPanel.setToolTipText("");

        jlUsername.setText("Username:");

        jlPassword.setText("Password:");

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfUsername)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlUsername)
                            .addComponent(jlPassword))
                        .addGap(0, 204, Short.MAX_VALUE))
                    .addComponent(jpfPassword))
                .addContainerGap())
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlUsername)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jlPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        explorePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Explore"));

        jlKeyword.setText("Keyword:");

        jtfKeyword.setToolTipText("(Name, Type, Comments, etc.)");

        jlLocation.setText("Starting Near:");

        jtfLocation.setToolTipText("(City, State, Postal, Country or Placename) ");

        javax.swing.GroupLayout explorePanelLayout = new javax.swing.GroupLayout(explorePanel);
        explorePanel.setLayout(explorePanelLayout);
        explorePanelLayout.setHorizontalGroup(
            explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(explorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfKeyword)
                    .addGroup(explorePanelLayout.createSequentialGroup()
                        .addGroup(explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlKeyword)
                            .addComponent(jlLocation))
                        .addGap(0, 219, Short.MAX_VALUE))
                    .addComponent(jtfLocation))
                .addContainerGap())
        );
        explorePanelLayout.setVerticalGroup(
            explorePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(explorePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlKeyword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jlLocation)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbSearch.setText("Search");
        jbSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSearchActionPerformed(evt);
            }
        });

        filtersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Filters"));
        filtersPanel.setToolTipText("");

        jlActivityType.setText("Activity Type:");

        jlEventType.setText("Event Type:");

        jlTimePeriod.setText("Time Period:");

        jcbTimePeriod.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTimePeriodActionPerformed(evt);
            }
        });

        jpCustomDates.setVisible(false);

        jLabel1.setText("Custom Dates:");

        jdcDateInit.setDateFormatString("MM/dd/yyyy");

        jdcDateEnd.setDateFormatString("MM/dd/yyyy");

        jLabel2.setText("-");

        javax.swing.GroupLayout jpCustomDatesLayout = new javax.swing.GroupLayout(jpCustomDates);
        jpCustomDates.setLayout(jpCustomDatesLayout);
        jpCustomDatesLayout.setHorizontalGroup(
            jpCustomDatesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1)
            .addGroup(jpCustomDatesLayout.createSequentialGroup()
                .addComponent(jdcDateInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdcDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jpCustomDatesLayout.setVerticalGroup(
            jpCustomDatesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpCustomDatesLayout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpCustomDatesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jdcDateInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jdcDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jlDistance.setText("Distance:");

        jLabel4.setText("-");

        jlTime.setText("Time:");

        jLabel6.setText("-");

        jlElevationGain.setText("Elevation Gain:");

        jLabel3.setText("-");

        jlDisplayName.setText("Display name:");

        jcbMyActivities.setText("My activities only");

        javax.swing.GroupLayout filtersPanelLayout = new javax.swing.GroupLayout(filtersPanel);
        filtersPanel.setLayout(filtersPanelLayout);
        filtersPanelLayout.setHorizontalGroup(
            filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filtersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jlActivityType, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlEventType, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlTimePeriod, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbEventType, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jcbTimePeriod, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpCustomDates, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jcbActivityType, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(57, 57, 57)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(filtersPanelLayout.createSequentialGroup()
                        .addComponent(jtfTimeInit, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfTimeEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlElevationGain)
                    .addGroup(filtersPanelLayout.createSequentialGroup()
                        .addComponent(jtfElevationInit, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfElevationEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(filtersPanelLayout.createSequentialGroup()
                        .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlDistance)
                            .addGroup(filtersPanelLayout.createSequentialGroup()
                                .addComponent(jtfDistanceInit, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtfDistanceEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jlTime))
                        .addGap(58, 58, 58)
                        .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfDisplayName)
                            .addGroup(filtersPanelLayout.createSequentialGroup()
                                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jcbMyActivities)
                                    .addComponent(jlDisplayName))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        filtersPanelLayout.setVerticalGroup(
            filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filtersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlActivityType)
                    .addComponent(jlDistance)
                    .addComponent(jlDisplayName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbActivityType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDistanceInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jtfDistanceEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDisplayName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlEventType)
                    .addComponent(jlTime)
                    .addComponent(jcbMyActivities))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbEventType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfTimeInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jtfTimeEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlTimePeriod)
                    .addComponent(jlElevationGain))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(filtersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbTimePeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfElevationInit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jtfElevationEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jpCustomDates, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpStorage.setBorder(javax.swing.BorderFactory.createTitledBorder("Storage"));

        jlTcx.setText("Tcx file:");

        jlXml.setText("Xml file:");
        
        jlGpx.setText("Gpx file:");

        jbTcxFolder.setText("Select Folder");
        jbTcxFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTcxFolderActionPerformed(evt);
            }
        });

        jbXmlFolder.setText("SelectFolder");
        jbXmlFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbXmlFolderActionPerformed(evt);
            }
        });
        
        jbGpxFolder.setText("SelectFolder");
        jbGpxFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGpxFolderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpStorageLayout = new javax.swing.GroupLayout(jpStorage);
        jpStorage.setLayout(jpStorageLayout);
        jpStorageLayout.setHorizontalGroup(
            jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStorageLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlTcx)
                    .addComponent(jlXml)
                    .addComponent(jlGpx))
                .addGap(18, 18, 18)
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtfTcx, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                    .addComponent(jtfXml)
                    .addComponent(jtfGpx))
                .addGap(18, 18, 18)
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbTcxFolder)
                    .addComponent(jbXmlFolder)
                    .addComponent(jbGpxFolder))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpStorageLayout.setVerticalGroup(
            jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStorageLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlTcx)
                    .addComponent(jtfTcx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbTcxFolder))
                .addGap(18, 18, 18)
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlXml)
                    .addComponent(jtfXml, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbXmlFolder))
                    .addGap(18, 18, 18)
                .addGroup(jpStorageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlGpx)
                    .addComponent(jtfGpx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbGpxFolder))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(245, 245, 245)
                        .addComponent(jbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jpStorage, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(filtersPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(explorePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(loginPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(explorePanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(filtersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpStorage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jcbTimePeriodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTimePeriodActionPerformed
        if(jcbTimePeriod.getSelectedIndex() == 4){//custom dates
            jpCustomDates.setVisible(true);
            
            if(!customDatesSelected){
                this.setSize(this.getWidth(), this.getHeight() + 60);
            }
            customDatesSelected = true;
        }else{
            jpCustomDates.setVisible(false);
            if(customDatesSelected){
                this.setSize(this.getWidth(), this.getHeight() - 60);
            }
            customDatesSelected = false;
        }
    }//GEN-LAST:event_jcbTimePeriodActionPerformed

    private void jbSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSearchActionPerformed
        search();
    }//GEN-LAST:event_jbSearchActionPerformed

    private void jbTcxFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbTcxFolderActionPerformed
        int result = tcxFile.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            tcxPath = tcxFile.getSelectedFile().getAbsolutePath();
            while(tcxPath.indexOf('\\') != -1){
                 tcxPath = tcxPath.replace('\\', '/');
            }
            this.jtfTcx.setText(tcxPath);
        }
    }//GEN-LAST:event_jbTcxFolderActionPerformed

    private void jbXmlFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbXmlFolderActionPerformed
        int result = xmlFile.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            xmlPath = xmlFile.getSelectedFile().getAbsolutePath();
            while(xmlPath.indexOf('\\') != -1){
                 xmlPath = xmlPath.replace('\\', '/');
            }
            this.jtfXml.setText(xmlPath);
        }
    }//GEN-LAST:event_jbXmlFolderActionPerformed

    private void jbGpxFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbXmlFolderActionPerformed
        int result = gpxFile.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            gpxPath = gpxFile.getSelectedFile().getAbsolutePath();
            while(gpxPath.indexOf('\\') != -1){
            	gpxPath = gpxPath.replace('\\', '/');
            }
            this.jtfGpx.setText(gpxPath);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel explorePanel;
    private javax.swing.JPanel filtersPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbSearch;
    private javax.swing.JButton jbTcxFolder;
    private javax.swing.JButton jbXmlFolder;
    private javax.swing.JButton jbGpxFolder;
    private javax.swing.JComboBox<OptionItem> jcbActivityType;
    private javax.swing.JComboBox<OptionItem> jcbEventType;
    private javax.swing.JCheckBox jcbMyActivities;
    private javax.swing.JComboBox<OptionItem> jcbTimePeriod;
    private com.toedter.calendar.JDateChooser jdcDateEnd;
    private com.toedter.calendar.JDateChooser jdcDateInit;
    private javax.swing.JLabel jlActivityType;
    private javax.swing.JLabel jlDisplayName;
    private javax.swing.JLabel jlDistance;
    private javax.swing.JLabel jlElevationGain;
    private javax.swing.JLabel jlEventType;
    private javax.swing.JLabel jlKeyword;
    private javax.swing.JLabel jlLocation;
    private javax.swing.JLabel jlPassword;
    private javax.swing.JLabel jlTcx;
    private javax.swing.JLabel jlTime;
    private javax.swing.JLabel jlTimePeriod;
    private javax.swing.JLabel jlUsername;
    private javax.swing.JLabel jlXml;
    private javax.swing.JLabel jlGpx;
    private javax.swing.JPanel jpCustomDates;
    private javax.swing.JPanel jpStorage;
    private javax.swing.JPasswordField jpfPassword;
    private javax.swing.JTextField jtfDisplayName;
    private javax.swing.JTextField jtfDistanceEnd;
    private javax.swing.JTextField jtfDistanceInit;
    private javax.swing.JTextField jtfElevationEnd;
    private javax.swing.JTextField jtfElevationInit;
    private javax.swing.JTextField jtfKeyword;
    private javax.swing.JTextField jtfLocation;
    private javax.swing.JTextField jtfTcx;
    private javax.swing.JTextField jtfTimeEnd;
    private javax.swing.JTextField jtfTimeInit;
    private javax.swing.JTextField jtfUsername;
    private javax.swing.JTextField jtfXml;
    private javax.swing.JTextField jtfGpx;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables

	@SuppressWarnings("unchecked")
	private void createActivityTypeComboBox() {
        ExtendedComboBox ex = (ExtendedComboBox) jcbActivityType;
        ex.addItem(new OptionItem("Any Activity Type", "all"));
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Running", "running"));
        
        ex.addItem(new OptionItem("     Street Running", "street_running"));
        ex.addItem(new OptionItem("     Track Running", "track_running"));
        ex.addItem(new OptionItem("     Trail Running", "trail_running"));
        ex.addItem(new OptionItem("     Treadmill Running", "treadmill_running"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Cycling", "cycling"));
        
        ex.addItem(new OptionItem("     Cyclocross", "cyclocross"));
        ex.addItem(new OptionItem("     Downhill Biking", "downhill_biking"));
        ex.addItem(new OptionItem("     Indoor Cycling", "indoor_cycling"));
        ex.addItem(new OptionItem("     Mountain Biking", "mountain_biking"));
        ex.addItem(new OptionItem("     Recumbent Cycling", "recumbent_cycling"));
        ex.addItem(new OptionItem("     Road Cycling", "road_biking"));
        ex.addItem(new OptionItem("     Track Cycling", "track_cycling"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Fitness Equipment", "fitness_equipment"));
        
        ex.addItem(new OptionItem("     Elliptical", "elliptical"));
        ex.addItem(new OptionItem("     Indoor Cardio", "indoor_cardio"));
        ex.addItem(new OptionItem("     Indoor Rowing", "indoor_rowing"));
        ex.addItem(new OptionItem("     Stair Climbing", "stair_climbing"));
        ex.addItem(new OptionItem("     Strength Training", "strength_training"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Hiking", "hiking"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Swimming", "swimming"));
        
        ex.addItem(new OptionItem("     Lap Swimming", "lap_swimming"));
        ex.addItem(new OptionItem("     Open Water Swimming", "open_water_swimming"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Walking", "walking"));
        
        ex.addItem(new OptionItem("     Casual Walking", "casual_walking"));
        ex.addItem(new OptionItem("     Speed Walking", "speed_walking"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Transition", "transition"));
        
        ex.addItem(new OptionItem("     Swim to Bike Transition", "swimToBikeTransition"));
        ex.addItem(new OptionItem("     Bike to Run Transition", "bikeToRunTransition"));
        ex.addItem(new OptionItem("     Run to Bike Transition", "runToBikeTransition"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Motorcycling", "motorcycling"));
        
        ex.addDelimiter("----------------------------------");
        ex.addItem(new OptionItem("Other", "other"));
        
        ex.addItem(new OptionItem("     Backcountry Skiing/Snowboarding", "backcountry_skiing_snowboarding"));
        ex.addItem(new OptionItem("     Boating", "boating"));
        ex.addItem(new OptionItem("     Cross Country Skiing", "cross_country_skiing"));
        ex.addItem(new OptionItem("     Driving", "driving_general"));
        ex.addItem(new OptionItem("     Flying", "flying"));
        ex.addItem(new OptionItem("     Golf", "golf"));
        ex.addItem(new OptionItem("     Horseback Riding", "horseback_riding"));
        ex.addItem(new OptionItem("     Inline Skating", "inline_skating"));
        ex.addItem(new OptionItem("     Mountaineering", "mountaineering"));
        ex.addItem(new OptionItem("     Paddling", "paddling"));
        ex.addItem(new OptionItem("     Resort Skiing/Snowboarding", "resort_skiing_snowboarding"));
        ex.addItem(new OptionItem("     Rowing", "rowing"));
        ex.addItem(new OptionItem("     Sailing", "sailing"));
        ex.addItem(new OptionItem("     Skate Skiing", "skate_skiing"));
        ex.addItem(new OptionItem("     Skating", "skating"));
        ex.addItem(new OptionItem("     Snowmobiling", "snowmobiling"));
        ex.addItem(new OptionItem("     Snowshoeing", "snow_shoe"));
        ex.addItem(new OptionItem("     Stand Up Paddleboarding", "stand_up_paddleboarding"));
        ex.addItem(new OptionItem("     Whitewater Kayaking/Rafting", "whitewater_rafting_kayaking"));
        ex.addItem(new OptionItem("     Wind/Kite Surfing", "wind_kite_surfing"));
        
    }

    private void createEventTypeComboBox() {       
        jcbEventType.addItem(new OptionItem("Any Event Type", "all"));
        jcbEventType.addItem(new OptionItem("Geocaching", "geocaching"));
        jcbEventType.addItem(new OptionItem("Fitness", "fitness"));
        jcbEventType.addItem(new OptionItem("Recreation", "recreation"));
        jcbEventType.addItem(new OptionItem("Race", "race"));
        jcbEventType.addItem(new OptionItem("Special Event", "specialEvent"));
        jcbEventType.addItem(new OptionItem("Training", "training"));
        jcbEventType.addItem(new OptionItem("Transportation", "transportation"));
        jcbEventType.addItem(new OptionItem("Touring", "touring"));
        jcbEventType.addItem(new OptionItem("Uncategorized", "uncategorized"));
    }

    private void createTimePeriodComboBox() {
        jcbTimePeriod.addItem(new OptionItem("Last 30 Days", "previousDays=30"));
        jcbTimePeriod.addItem(new OptionItem("Last 24 Hours", "previousDays=1"));
        jcbTimePeriod.addItem(new OptionItem("Last 7 Days", "previousDays=7"));
        jcbTimePeriod.addItem(new OptionItem("Last 365 Days", "previousDays=365"));
        jcbTimePeriod.addItem(new OptionItem("Custom Dates", "customTimestamp"));
    }

    private void createComboBoxes() {
        createActivityTypeComboBox();
        createEventTypeComboBox();
        createTimePeriodComboBox();
    }

    private void createComponents() {
        createComboBoxes();
        
    }

    private void search() {
        this.setVisible(false);
        Data data = Data.getInstance();
        
        if(!"".equals(xmlPath.trim()) && !xmlPath.endsWith("/")) xmlPath += "/";
        data.setXmlFolder(xmlPath);
        if(!"".equals(tcxPath.trim()) && !tcxPath.endsWith("/")) tcxPath += "/";
        data.setTcxFolder(tcxPath);
        if(!"".equals(gpxPath.trim()) && !gpxPath.endsWith("/")) gpxPath += "/";
        data.setGpxFolder(gpxPath);
        
        data.setKeyword(jtfKeyword.getText());
        data.setLocation(jtfLocation.getText());
        
        OptionItem item = (OptionItem) jcbActivityType.getSelectedItem();     
        data.setActivityType(item.getValue());
        
        item = (OptionItem) jcbEventType.getSelectedItem();     
        data.setEventType(item.getValue());
        
        item = (OptionItem) jcbTimePeriod.getSelectedItem();     
        data.setTimePeriod(item.getValue());
        
        if(item.getValue().equals(Constants.CUSTOM_DATE)){
//            if(!"MM/dd/yyyy".equals(jdcDateInit.getDateFormatString())){
            Date date = jdcDateInit.getDate();
            
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
            if(date != null){
                data.setDateInit(df.format(date));
            }else{
                data.setDateInit("");
            }
            
            date = jdcDateEnd.getDate();
            if(date != null){
                data.setDateEnd(df.format(date));
            }else{
                data.setDateEnd("");
            }
//            }
//            if(!"MM/dd/yyyy".equals(jdcDateEnd.getDateFormatString())){
//                data.setDateEnd(jdcDateEnd.getDate().toString());
//            }
            
        }
        
        data.setDistanceInit(jtfDistanceInit.getText());
        data.setDistanceEnd(jtfDistanceEnd.getText());
        
        data.setTimeInit(jtfTimeInit.getText());
        data.setTimeEnd(jtfTimeEnd.getText());
        
        data.setElevationInit(jtfElevationInit.getText());
        data.setElevationEnd(jtfElevationEnd.getText());
        
        data.setDisplayName(jtfDisplayName.getText());
        
        data.setMyActivitiesOnly(jcbMyActivities.isSelected());
        
        if(!"".equals(jtfUsername.getText().trim())){
            data.setUserName(jtfUsername.getText());
        }
        
        char[] ch = jpfPassword.getPassword();
        String pass = new String(ch);
        
        if(!"".equals(pass)){
            data.setPassword(pass);
        }
        
        new WebCrawler().start();
        
    }
}

